//
//  ZYRefreshControl.swift
//  weiboZY
//
//  Created by 赵勇 on 16/5/18.
//  Copyright © 2016年 赵勇. All rights reserved.
//

import UIKit

enum RefreshedState: Int {
    case Nomal = 0
    case Pulling = 1
    case Refreshing = 2
}


class ZYRefreshControl: UIControl {

    // 当前控件状态
    var currentRefreshState:RefreshedState = .Nomal {
        didSet{
            
            switch currentRefreshState {
                
            case .Nomal:
                print("nomal")
                
                if oldValue == .Refreshing {
                    
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.currentScorllView?.contentInset.top -= 50
                    })
                }
                
            case.Pulling:
                print("pull")
            case.Refreshing:
                print("re")
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.currentScorllView?.contentInset.top += 50
                })
            // 告诉使用者刷新数据
                sendActionsForControlEvents(.ValueChanged)
                
            }
        }
    }
    
    var currentScorllView:UIScrollView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor.yellowColor()
    }

    // 获取当前控件添加到哪个父视图上
    override func willMoveToSuperview(newSuperview: UIView?) {
//        print(newSuperview)
        
        guard let scorllView = newSuperview as? UIScrollView else {
            return
        }
    // 添加kvo监听
        scorllView.addObserver(self, forKeyPath: "contentOffset", options: [.New], context: nil)
        
        currentScorllView = scorllView
        
        // 设置frame
        frame.size.height = 50
        frame.size.width = currentScorllView!.frame.size.width
        frame.origin.y = -50
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        print(currentScorllView?.contentOffset.y ?? 0)
        // 拖动的时候分两种状态 1 下来刷新状态 2 松手刷新状态
        if currentScorllView!.dragging {
            // 拖动
           let maxY = -(currentScorllView!.contentInset.top + 50)
            
           let currentY = currentScorllView!.contentOffset.y
            if currentY < maxY && currentRefreshState == .Nomal{
                // 进入pulling zhuangtai
                currentRefreshState = .Pulling
            } else if currentY >= maxY && currentRefreshState == .Pulling {
                // 进入 nomal 状态
                currentRefreshState = .Nomal
            }
            
        } else {
            // 松手状态
            
            if currentRefreshState == .Pulling {
                
                currentRefreshState = .Refreshing
                
            }
            
      
            
        }
        
        
    }
    
    deinit {
        // 移除kvo
        currentScorllView?.removeObserver(self, forKeyPath: "contentOffset")
    }
    
}





